#!/bin/bash

# 定义颜色变量
red='\033[0;31m'
green='\033[0;32m'
blue='\033[0;34m'
yellow='\033[0;33m'
plain='\033[0m'

# 获取当前目录
cur_dir=$(pwd)

# 检查是否为root用户
[[ $EUID -ne 0 ]] && echo -e "${red}错误: ${plain} 请使用root用户运行此脚本\n " && exit 1

# 检查系统类型
if [[ -f /etc/os-release ]]; then
    source /etc/os-release
    release=$ID
elif [[ -f /usr/lib/os-release ]]; then
    source /usr/lib/os-release
    release=$ID
else
    echo "无法检测系统类型，请联系作者！" >&2
    exit 1
fi
echo "系统类型: $release"

# 检查CPU架构
arch() {
    case "$(uname -m)" in
    x86_64 | x64 | amd64) echo 'amd64' ;;
    armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
    *) echo -e "${red}不支持的CPU架构！${plain}" && rm -f install.sh && exit 1 ;;
    esac
}

echo "CPU架构: $(arch)"

# 安装基础软件包
install_base() {
    case "${release}" in
    ubuntu | debian)
        apt-get update && apt-get install -y -q wget curl tar tzdata
        ;;
    centos)
        yum -y update && yum install -y -q wget curl tar tzdata
        ;;
    *)
        apt-get update && apt install -y -q wget curl tar tzdata
        ;;
    esac
}

# 生成随机字符串
gen_random_string() {
    local length="$1"
    local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
    echo "$random_string"
}

# 获取服务器IP地址
get_ip() {
    local response=$(curl -s --max-time 3 https://api.ec.mk/cdn-cgi/trace)
    if [ -n "$response" ]; then
        echo "$response" | grep "ip=" | cut -d= -f2
    fi
}

# 安装后配置
config_after_install() {
    local config_username=$(gen_random_string 10)
    local config_password=$(gen_random_string 10)
    local config_port=$(shuf -i 1024-62000 -n 1)
    local config_webBasePath=$(gen_random_string 18)
    local server_ip=$(get_ip)
    if [ -z "$server_ip" ]; then
        server_ip="未知"
        echo -e "${yellow}警告: 无法获取服务器IP地址${plain}"
    fi

    /usr/local/x-ui/x-ui setting -username "${config_username}" -password "${config_password}" -port "${config_port}" -webBasePath "${config_webBasePath}"
    echo -e "安装完成，以下是您的登录信息："
    echo -e "###############################################"
    echo -e "${green}用户名: ${config_username}${plain}"
    echo -e "${green}密码: ${config_password}${plain}"
    echo -e "${green}端口: ${config_port}${plain}"
    echo -e "${green}网页路径: ${config_webBasePath}${plain}"
    echo -e "${green}访问地址: http://${server_ip}:${config_port}/${config_webBasePath}${plain}"
    echo -e "###############################################"

    /usr/local/x-ui/x-ui migrate
}

# 安装x-ui主程序
install_x-ui() {
    cd /usr/local/

    # 根据架构下载对应的文件
    if [[ $(arch) == "amd64" ]]; then
        wget -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://gitcode.com/meu/3x-ui/releases/download/v2.6.2/x-ui-linux-amd64.tar.gz
    else
        wget -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://gitcode.com/meu/3x-ui/releases/download/v2.6.2/x-ui-linux-arm64.tar.gz
    fi

    if [[ $? -ne 0 ]]; then
        echo -e "${red}下载x-ui失败，请检查网络连接${plain}"
        exit 1
    fi

    # 如果已存在则删除
    if [[ -e /usr/local/x-ui/ ]]; then
        systemctl stop x-ui
        rm /usr/local/x-ui/ -rf
    fi

    # 解压安装
    tar zxvf x-ui-linux-$(arch).tar.gz
    rm x-ui-linux-$(arch).tar.gz -f
    cd x-ui
    chmod +x x-ui bin/xray-linux-$(arch)
    cp -f x-ui.service /etc/systemd/system/
    wget -O /usr/bin/x-ui https://raw.gitcode.com/meu/3x-ui/raw/main/x-ui.sh
    chmod +x /usr/local/x-ui/x-ui.sh
    chmod +x /usr/bin/x-ui
    
    # 配置并启动服务
    config_after_install
    systemctl daemon-reload
    systemctl enable x-ui
    systemctl start x-ui
    
    echo -e "${green}x-ui 安装完成，已启动运行...${plain}"
    echo -e ""
    echo -e "x-ui 管理脚本使用方法: "
    echo -e "----------------------------------------------"
    echo -e "x-ui              - 显示管理菜单"
    echo -e "x-ui start        - 启动 x-ui 面板"
    echo -e "x-ui stop         - 停止 x-ui 面板"
    echo -e "x-ui restart      - 重启 x-ui 面板"
    echo -e "x-ui status       - 查看 x-ui 状态"
    echo -e "x-ui enable       - 设置 x-ui 开机自启"
    echo -e "x-ui disable      - 取消 x-ui 开机自启"
    echo -e "x-ui log          - 查看 x-ui 日志"
    echo -e "x-ui update       - 更新 x-ui 面板"
    echo -e "x-ui install      - 安装 x-ui 面板"
    echo -e "x-ui uninstall    - 卸载 x-ui 面板"
    echo -e "----------------------------------------------"
}

echo -e "${green}开始运行...${plain}"
install_base
install_x-ui
